from pymongo import MongoClient
from bson.objectid import ObjectId
from functools import wraps
import psutil
from pymongo import errors


class TestMongo(object):
    def __init__(self):
        # 这里如果数据库在本机, 那么不需要登录, 如果数据库在别的机器那么需要用户和密码
        self.client = MongoClient('mongodb://synjones:synjones2021@127.0.0.1:27017/', serverSelectionTimeoutMS=3000)
        self.client.admin.command('ismaster')
        print('连接mongo成功')

    def delete_one(self):
        post ={
            'userName': 'yinlu'
        }
        res=self.client['synPos']['userList'].delete_one(post)
        #当使用WriteConcern为None时，该属性将为False，否则为True。  delete_one接口里默认是write_concern不为none    WriteConcern的意思是写入关注
        print(res.acknowledged)# 
        print(res.deleted_count)# 删除的个数
        print(res.raw_result)#原始返回结果

    def insert_one(self):
        post = {'userName': 'yinlu1'}
        res = self.client['synPos']['userList'].insert_one(post)
        #print(res.inserted_id)
        print(res.acknowledged)
        print(res.inserted_id)# 返回的自增字段

    def insert_many(self):
            
        infos = [
            {'name': 'ben', 'age': 18, 'sex': "male", 'grade': 80, 'time': '855555221220'},
            {'name': 'ss', 'age': 19, 'sex': "male", 'grade': 75, 'time': '855555221221'},
            {'name': 'wr', 'age': 16, 'sex': "female", 'grade': 90, 'time': '855555221222'},
            {'name': 'wwe', 'age': 19, 'sex': "male", 'grade': 65, 'time': '855555221223'},
            {'name': 'sa', 'age': 18, 'sex': "female", 'grade': 80, 'time':'855555221224'}
            ]
        res=self.client['synPos']['userList'].insert_many(infos )
        print(res.acknowledged)# 
        print(res.inserted_ids)# 返回的自增字段们

    #替换是找到该数据  全部替换为指定数据  不存在则创建
    def replace_one(self):
            
        res = self.client['synPos']['userList'].replace_one({'name': 'dsadasdas'}, {'dsad': 'wewe'})
        print(res.acknowledged)# 
        print(res.matched_count)# 与此匹配的文档数量。  
        print(res.modified_count)#修改文档的数量。
        print(res.raw_result)#原始返回结果

    #更新是找到该数据  更新某一个字段  不存在则创建
    def update_one(self):    
        res = self.client['synPos']['userList'].update_one({'name': 'sa'}, {'$set': {'dsad': 'wewe'}})
        print(res.acknowledged)# 
        print(res.matched_count)# 与此匹配的文档数量。  
        print(res.modified_count)#修改文档的数量。
        print(res.raw_result)#原始返回结果

    def find_one_and_delete(self):
        cond = {'userName': 'yinlu1'}
        result = self.client['synPos']['userList'].find_one_and_delete(cond)
        print(result)

    def find_one_and_replace(self):
        result = self.client['synPos']['userList'].find_one_and_replace({'name': 'ss'} ,{'sadasdsa': 'wewqeqwewqeqweqwewe'} )
        print(result)

    def find_one_and_update(self):   
        result = self.client['synPos']['userList'].find_one_and_update({'name': '33'} , {'$set': {'dsad': 'wewe'}} )
        print(result)

def main():
    obj = TestMongo()
    # obj.insert_one()
    obj.find_one_and_delete()
   
if __name__ == '__main__':
    main()

